import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    allowedHosts: [
      'localhost',
      '127.0.0.1',
      '.ngrok.io',
      '.ngrok-free.app',
      '.ngrok.app'
    ],
    host: true, // 允许外部访问
    proxy: {
      '/api/lightcc': {
        target: 'https://api.lightcc.cloud',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/lightcc/, '/system/api/v1'),
        configure: (proxy: any, _options: any) => {
          proxy.on('error', (_err: any, _req: any, _res: any) => {
            // console.log('proxy error', err);
          });
          proxy.on('proxyReq', (proxyReq: any, _req: any, _res: any) => {
            // console.log('Sending Request to the Target:', req.method, req.url);
            // 添加API密钥到请求头
            proxyReq.setHeader('X-Api-Key', '6948fec62ff649848bc0be21a49275f6');
          });
          proxy.on('proxyRes', (_proxyRes: any, _req: any, _res: any) => {
            // console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
          });
        },
      }
    }
  },
})
